package com.learn.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * kafka 消费者
 *
 * @Author: cong zhi
 * @CreateDate: 2021/3/2 15:38
 * @UpdateUser: cong zhi
 * @UpdateDate: 2021/3/2 15:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Component
public class KafkaDynamicConsumer {



    @KafkaListener(topics = {"test-topic"}, groupId = "demo-consumer-group")
    public void listener(ConsumerRecord<String, String> record) {

        Optional<String> messages = Optional.ofNullable(record.value());
        if (messages.isPresent()) {
            System.out.printf("消费者实例 [%s] 接收到消息：%s%n",
                    Thread.currentThread().getName(), record.value());
        }
    }
}
